package com.plm.frame.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.plm.frame.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.plm.frame.core.Constants;
import com.plm.frame.model.User;

@Controller
public class UserAction extends BaseAction{

	@Resource
	private UserService userService;
	
	@RequestMapping("/gerenxinxi")
	public String gerenxinxi(Map<String,List> m){
		User user = new User();
		user.setUsername(((User) getSession().getAttribute(Constants.LOGIN_USER)).getUsername());
		m.put("info", userService.selectUser(user));
		return "gereninfo";
	}
	
	@RequestMapping("/login")
	public String login(Map<String,List> m){
		return "login";
	}
	
	@RequestMapping("/regist")
	public String regist(Map<String,List> m){
		return "regist";
	}
	
	@RequestMapping("/editGerenxinxi")
	public String editGerenxinxi(Map<String,List> m,User user){
		user.setUsername(((User) getSession().getAttribute(Constants.LOGIN_USER)).getUsername());
		userService.editUser(user);
		return "editsuccess";
	}
	
	@RequestMapping("/saveUser")
	public String saveUser(Map<String,List> m,User user){
		if(userService.selectUser(user).size()>0)
			return "registfailed";
		else
			userService.saveUser(user);
		return "registsuccess";
	}
	
	@RequestMapping("/userLogin")
	public String userLogin(User user,HttpServletRequest request){
		User rUser=userService.searchUser(user);
		if(rUser!=null){
			request.getSession().setAttribute(Constants.LOGIN_USER, rUser);
			return "success";
		}
		else{
			return "failed";
		}
	}
	
	@RequestMapping("/logout")
	public String logout(Map<String,List> m,HttpSession session) {
		getSession().invalidate();
		return "redirect:index";
	}

}
